package com.new_wear.project.activity.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.new_wear.common.exception.BusinessException;
import com.new_wear.common.utils.file.FileUploadUtils;
import com.new_wear.framework.config.NewEraConfig;
import com.new_wear.project.system.menu.domain.Menu;
import com.new_wear.project.system.menu.service.IMenuService;
import com.new_wear.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.new_wear.framework.aspectj.lang.annotation.Log;
import com.new_wear.framework.aspectj.lang.enums.BusinessType;
import com.new_wear.project.activity.domain.CstActivity;
import com.new_wear.project.activity.service.ICstActivityService;
import com.new_wear.framework.web.controller.BaseController;
import com.new_wear.framework.web.domain.AjaxResult;
import com.new_wear.common.utils.poi.ExcelUtil;
import com.new_wear.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 活动Controller
 * 
 * @author new_wear
 * @date 2020-01-07
 */
@Controller
@RequestMapping("/activity/activity")
public class CstActivityController extends BaseController
{
    private String prefix = "activity/activity";

    @Autowired
    private ICstActivityService cstActivityService;

    @Autowired
    private IMenuService menuService;

    @RequiresPermissions("activity:activity:view")
    @GetMapping("/{menuId}")
    public String activity(ModelMap mmap, @PathVariable("menuId") Integer menuId)
    {
        mmap.put("menuId", menuId);
        return prefix + "/activity";
    }

    /**
     * 查询活动列表
     */
    @RequiresPermissions("activity:activity:list")
    @PostMapping("/list/{menuId}")
    @ResponseBody
    public TableDataInfo list(CstActivity cstActivity,@PathVariable("menuId") Integer menuId)
    {
        startPage();
        cstActivity.setCategory(menuId);
        List<CstActivity> list = cstActivityService.selectCstActivityList(cstActivity);
        for (CstActivity activity:list) {
            int i = activity.getStartTime().compareTo(new Date());
            int j = activity.getEndTime().compareTo(new Date());
            if (i == 0 || i < 0 && j>0 || j == 0){
                activity.setStatus(1);
            }else if (i >0){
                activity.setStatus(0);
            }else if(j<0){
                activity.setStatus(2);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出活动列表
     */
    @RequiresPermissions("activity:activity:export")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CstActivity cstActivity)
    {
        List<CstActivity> list = cstActivityService.selectCstActivityList(cstActivity);
        ExcelUtil<CstActivity> util = new ExcelUtil<CstActivity>(CstActivity.class);
        return util.exportExcel(list, "activity");
    }

    /**
     * 新增活动
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap) {
        Menu menu = menuService.selectMenuById(id);
        mmap.put("menu", menu);
        User user = getSysUser();
        mmap.put("user", user);
        return prefix + "/add";
    }


    /**
     * 新增保存活动
     */
    @RequiresPermissions("activity:activity:add")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CstActivity cstActivity,@RequestParam("file") MultipartFile file) throws IOException {
        // 上传文件路径
        String filePath = NewEraConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        cstActivity.setImages(fileName);
        int i = cstActivity.getStartTime().compareTo(new Date());
        int j = cstActivity.getEndTime().compareTo(new Date());
        if (i == 0 || i < 0 && j>0 || j == 0){
            cstActivity.setStatus(1);
        }else if (i >0){
            cstActivity.setStatus(0);
        }else if(j<0){
            cstActivity.setStatus(2);
        }
        return toAjax(cstActivityService.insertCstActivity(cstActivity));
    }

    /**
     * 修改活动
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        CstActivity cstActivity = cstActivityService.selectCstActivityById(id);
        mmap.put("cstActivity", cstActivity);
        return prefix + "/edit";
    }

    /**
     * 修改保存活动
     */
    @RequiresPermissions("activity:activity:edit")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CstActivity cstActivity)
    {
        cstActivity.setIsCheck(0);
        return toAjax(cstActivityService.updateCstActivity(cstActivity));
    }

    /**
     * 删除活动
     */
    @RequiresPermissions("activity:activity:remove")
    @Log(title = "活动", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cstActivityService.deleteCstActivityByIds(ids));
    }
    /**
     * 修改终审状态
     */
    @GetMapping("/check/{id}")
    public String check(@PathVariable("id") Integer id, ModelMap mmap) {
        CstActivity cstActivity = cstActivityService.selectCstActivityById(id);
        User user = getSysUser();
        mmap.put("cstActivity", cstActivity);
        mmap.put("user", user);
        return prefix + "/check";
    }

    /**
     * 修改保存审核状态
     */
    @RequiresPermissions("article:article:check")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PostMapping("/check")
    @ResponseBody
    public AjaxResult checkSave(CstActivity cstActivity) {
        if (cstActivity.getIsCheck() == 2) {
            if (cstActivity.getCheckCase() == null || cstActivity.getCheckCase().isEmpty()) {
                throw new BusinessException("不通过的原因不能为空");
            }
            System.out.println("CstActivityController.checkSave=====" + cstActivity.getCheckCase());
        }
        return toAjax(cstActivityService.updateCstActivity(cstActivity));
    }

    /**
     * 取消审核
     */
    @RequiresPermissions("article:article:check")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PostMapping("/cancelCheck")
    @ResponseBody
    public AjaxResult cancelFinalCheck(Integer id) {
        CstActivity cstActivity = cstActivityService.selectCstActivityById(id);
        cstActivity.setIsCheck(0);
        cstActivity.setIsPublish(0);
        cstActivity.setCheckCase(null);
        return toAjax(cstActivityService.updateCstActivity(cstActivity));
    }

    /**
     * 修改发布状态
     */
    @RequiresPermissions("article:article:update")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PostMapping("/publish")
    @ResponseBody
    public AjaxResult publish(Integer id) {
        return toAjax(cstActivityService.publishCstActivity(id));
    }

}
